﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using WebApi001.Domain;
using WebApi001.Domain.Entity;
using Microsoft.Extensions.DependencyInjection;
using System.Runtime.Serialization;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel;
using WebApi001.Domain.Helper;

namespace WebApi001.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        //依赖注入
        //方法一
        private readonly Admin5000DbContext _db;

        [ActivatorUtilitiesConstructor]
        public WeatherForecastController(Admin5000DbContext dbContext)
        {
            _db = dbContext;
        }

        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        //方法二：直接注释构造函数logger

        //方法三：将两个构造函数写在一起
        //public WeatherForecastController(ILogger<WeatherForecastController> logger，Admin5000DbContext _db)
        //{
        //    _logger = logger;
        //    _db = dbContext;
        //}
        [HttpGet]
        public string Get()
        {
            //var res = _db.Products.Include(x=>x.Brand).ToList();加Include(x=>x.Brand.)就会有一大串的Brand值
            var res = _db.Products.ToList();
            //json 串行
            return JsonHelper.SerializeObject(res);
        }
    }
}
